package com.sf.appointment.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.sf.appointment.domain.BusAppointment;
import com.sf.appointment.domain.BusStatement;
import com.sf.appointment.mapper.BusAppointmentMapper;
import com.sf.appointment.mapper.BusStatementMapper;
import com.sf.appointment.vo.StatementItemVo;
import com.sf.common.utils.DateUtils;
import com.sf.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sf.appointment.mapper.BusStatementItemMapper;
import com.sf.appointment.domain.BusStatementItem;
import com.sf.appointment.service.IBusStatementItemService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 结算单明细Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-23
 */
@Transactional
@Service
public class BusStatementItemServiceImpl implements IBusStatementItemService
{
    @Autowired
    private BusStatementItemMapper busStatementItemMapper;

    @Autowired
    private BusStatementMapper busStatementMapper;

    @Autowired
    private BusAppointmentMapper busAppointmentMapper;

    /**
     * 查询结算单明细
     *
     * @param id 结算单明细主键
     * @return 结算单明细
     */
    @Override
    public BusStatementItem selectBusStatementItemById(Long id)
    {
        return busStatementItemMapper.selectBusStatementItemById(id);
    }

    /**
     * 查询结算单明细列表
     *
     * @param busStatementItem 结算单明细
     * @return 结算单明细
     */
    @Override
    public List<BusStatementItem> selectBusStatementItemList(BusStatementItem busStatementItem)
    {
        return busStatementItemMapper.selectBusStatementItemList(busStatementItem);
    }

    /**
     * 新增结算单明细
     *
     * @param busStatementItem 结算单明细
     * @return 结果
     */
    @Override
    public int insertBusStatementItem(BusStatementItem busStatementItem)
    {
        return busStatementItemMapper.insertBusStatementItem(busStatementItem);
    }

    /**
     * 修改结算单明细
     *
     * @param busStatementItem 结算单明细
     * @return 结果
     */
    @Override
    public int updateBusStatementItem(BusStatementItem busStatementItem)
    {
        return busStatementItemMapper.updateBusStatementItem(busStatementItem);
    }

    /**
     * 批量删除结算单明细
     *
     * @param ids 需要删除的结算单明细主键
     * @return 结果
     */
    @Override
    public int deleteBusStatementItemByIds(Long[] ids)
    {
        return busStatementItemMapper.deleteBusStatementItemByIds(ids);
    }

    /**
     * 删除结算单明细信息
     *
     * @param id 结算单明细主键
     * @return 结果
     */
    @Override
    public int deleteBusStatementItemById(Long id)
    {
        return busStatementItemMapper.deleteBusStatementItemById(id);
    }

    @Override
    public int save(StatementItemVo statementItemVo) {
        List<BusStatementItem> busStatementItems = statementItemVo.getBusStatementItems();
        if (busStatementItems == null || busStatementItems.size() == 0) {
            throw new RuntimeException("服务错误");
        }
        Long statementId = busStatementItems.get(0).getStatementId();
        BusStatement busStatement = busStatementMapper.selectBusStatementById(statementId);
        if(busStatement == null) {
            throw new RuntimeException("id不合法");
        }
        if(busStatement.getStatus().equals(BusStatement.STATUS_PAID)){
            throw new RuntimeException("状态不合法");
        }
        busStatementItemMapper.deleteBusStatementItemByStatementId(statementId);
        BigDecimal totalPrice = new BigDecimal(0);
        BigDecimal totalCount = new BigDecimal(0);
        for (BusStatementItem busStatementItem : busStatementItems) {
            BigDecimal itemPrice = busStatementItem.getItemPrice();
            Long itemQuantity = busStatementItem.getItemQuantity();
            totalPrice = totalPrice.add(itemPrice.multiply(new BigDecimal(itemQuantity)));
            totalCount=totalCount.add(new BigDecimal(itemQuantity));
            BusStatementItem busStatementItem1=new BusStatementItem();
            Long itemId = busStatementItem.getItemId();
            String itemName = busStatementItem.getItemName();
            busStatementItem1.setStatementId(statementId);
            busStatementItem1.setItemPrice(itemPrice);
            busStatementItem1.setItemQuantity(itemQuantity);
            busStatementItem1.setItemName(itemName);
            busStatementItem1.setItemId(itemId);
            busStatementItemMapper.insertBusStatementItem(busStatementItem1);
        }
        busStatement.setTotalAmount(totalPrice);
        busStatement.setTotalQuantity(totalCount);
        if(statementItemVo.getDiscountPrice()!=null&&statementItemVo.getDiscountPrice().compareTo(BigDecimal.ZERO)>0){
            BigDecimal discountPrice = statementItemVo.getDiscountPrice();
            if(discountPrice.compareTo(totalPrice)>0){
                throw new RuntimeException("折扣金额不能大于总价");
            }
            busStatement.setDiscountAmount(statementItemVo.getDiscountPrice());
        }
        Long appointmentId = busStatement.getAppointmentId();
        if(appointmentId!=null){
            BusAppointment busAppointment = busAppointmentMapper.selectBusAppointmentById(appointmentId);

            if (busAppointment == null) {
                throw new RuntimeException("id不合法");
            }
//            if (busAppointment.getStatus().equals(BusAppointment.STATUS_ARRIVAL)||busAppointment.getStatus().equals(BusAppointment.STATUS_SETTLE)){
//                busAppointment.setStatus(BusAppointment.STATUS_SETTLE);
//                busAppointmentMapper.updateBusAppointment(busAppointment);
//            }else{
//                throw new RuntimeException("状态不合法");
//            }
        }
        return busStatementMapper.updateBusStatement(busStatement);

    }

    @Override
    public int payBusStatementByIds(Long id) {
        BusStatement busStatement = busStatementMapper.selectBusStatementById(id);
        if(busStatement==null){
            throw new RuntimeException("id不合法");
        }
        if(busStatement.getStatus().equals(BusStatement.STATUS_PAID)){
            throw new RuntimeException("当前状态不能操作");
        }
        busStatement.setPayeeId(SecurityUtils.getUserId());
        busStatement.setPayTime(DateUtils.getNowDate());
        busStatement.setStatus(BusStatement.STATUS_PAID);
        BigDecimal totalAmount = busStatement.getTotalAmount();
        if (totalAmount==null){
            throw new RuntimeException("请先保存");
        }
        if (busStatement.getAppointmentId()!=null){
            BusAppointment busAppointment = busAppointmentMapper.selectBusAppointmentById(busStatement.getAppointmentId());
            if (busAppointment==null){
                throw new RuntimeException("操作不合法");
            }
            if (!busAppointment.getStatus().equals(BusAppointment.STATUS_SETTLE)){
                throw new RuntimeException("状态不合法");
            }
            busAppointment.setStatus(BusAppointment.STATUS_PAYED);
            busAppointmentMapper.updateBusAppointment(busAppointment);
        }
        return busStatementMapper.updateBusStatement(busStatement);
    }
}
